package com.example.childrendapp;

import java.util.ArrayList;

import com.example.childrendapp.MyCount.CountTimer;
import com.example.childrendapp.database.DataBaseHelper;
import com.example.childrendapp.totalscore.AppTotalScore;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class StartGame extends Activity implements OnClickListener {
	ArrayList<Maths> Ma;
	private DataBaseHelper mDB;
	private SQLiteDatabase myDatabase;
	private TextView tv1, tv2, tv3, tv4, tv5;
	private TextView mCountTime, mScore;
	private TextView mAnswer1, mAnswer2, mAnswer3, mAnswer4;
	private Cursor mCursor;
	String kq;
	private static int index = 0, rowcount, count = 0;
	long millisUntilFinished, TimeFail;
	private String TAG = "this is Question value :";
	private boolean checkpass = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.layout_app_startappmath);
		mDB = new DataBaseHelper(this);

		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		tv3 = (TextView) findViewById(R.id.tv3);
		tv4 = (TextView) findViewById(R.id.tv4);
		tv5 = (TextView) findViewById(R.id.tv5);
		mCountTime = (TextView) findViewById(R.id.countTime);
		mScore = (TextView) findViewById(R.id.score);
		mAnswer1 = (TextView) findViewById(R.id.answer1);
		mAnswer2 = (TextView) findViewById(R.id.answer2);
		mAnswer3 = (TextView) findViewById(R.id.answer3);
		mAnswer4 = (TextView) findViewById(R.id.answer4);
		mAnswer1.setOnClickListener(this);
		mAnswer2.setOnClickListener(this);
		mAnswer3.setOnClickListener(this);
		mAnswer4.setOnClickListener(this);
		CountTimer counter = new CountTimer(100000, 1000);
		counter.start();
		index = 0;
		try {
			mDB.createDataBase();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mDB.openDataBase();
		try {
			mDB.copyDataBase();
		} catch (Exception e) {
			e.printStackTrace();
		}
		myDatabase = mDB.getWritableDatabase();

		loadData();
	}

	@Override
	protected void onResume() {

		loadDataToView(index);
		Log.i(TAG, "Hoai " + mCursor.getCount());
		super.onResume();

	}

	public void Check() {
		if (checkpass == true) {
			Toast.makeText(this, "Pass", Toast.LENGTH_SHORT).show();
			count++;
			mScore.setText("Score " + count);
			if (index < rowcount - 1) {
				index++;
				RefreshData();
			}

		} else {
			Toast.makeText(this, "Fail", Toast.LENGTH_SHORT).show();

		}
		Log.i("Count :", "" + count);
		// chuyen Activity khi count = 5

		if (count == 5) {
			Intent nextScreen = new Intent(this, AppTotalScore.class);
			nextScreen.putExtra("Score", "" + count);
			startActivity(nextScreen);

		}
	}

	@Override
	public void onClick(View v) {
		if (v == mAnswer1) {
			if (mAnswer1.getText().toString().equals(kq)) {

				checkpass = true;
			} else {
				checkpass = false;

			}
		}
		if (v == mAnswer2) {
			if (mAnswer2.getText().toString().equals(kq)) {
				checkpass = true;
			} else {
				checkpass = false;
			}
		}
		if (v == mAnswer3) {
			if (mAnswer3.getText().toString().equals(kq)) {
				checkpass = true;
			} else {
				checkpass = false;
			}
		}
		if (v == mAnswer4) {
			if (mAnswer4.getText().toString().equals(kq)) {
				checkpass = true;
			} else {
				checkpass = false;
			}
		}
		Check();
	}

	private void RefreshData() {

		// mMaths=new Maths(question, ans1, ans2, ans3, ans4, result);
		tv1.setText("");
		tv2.setText("");
		tv3.setText("");
		tv4.setText("");
		tv5.setText("");
		mAnswer1.setText("");
		mAnswer2.setText("");
		mAnswer3.setText("");
		mAnswer4.setText("");
		loadDataToView(index);
		/*
		 * for(int i=0;i<mCursor.getCount();i++){
		 * 
		 * i++; }
		 */
	}

	private void loadDataToView(int index) {
		Maths item = Ma.get(index);
		char a[] = item.getQuestion().toCharArray();
		tv1.setText("" + a[0]);
		tv2.setText("" + a[1]);
		tv3.setText("" + a[2]);
		tv4.setText("" + a[3]);
		tv5.setText("" + a[4]);
		mAnswer1.setText("" + item.getAnswer1());
		mAnswer2.setText("" + item.getAnswer2());
		mAnswer3.setText("" + item.getAnswer3());
		mAnswer4.setText("" + item.getAnswer4());
		kq = item.getResult();
	}

	private void loadData() {

		// Maths m = new Maths();
		mCursor = myDatabase.rawQuery("SELECT * FROM MATHS", null);
		rowcount = mCursor.getCount();
		if (mCursor != null && mCursor.moveToFirst()) {
			Ma = new ArrayList<Maths>();

			// while(mCursor.isAfterLast()==false){
			for (int i = 0; i < mCursor.getCount(); i++) {
				Maths m = new Maths();
				m.setQuestion(mCursor.getString(mCursor
						.getColumnIndex("Question")));
				m.setAnswer1(mCursor.getString(mCursor
						.getColumnIndex("Answer1")));
				m.setAnswer2(mCursor.getString(mCursor
						.getColumnIndex("Answer2")));
				m.setAnswer3(mCursor.getString(mCursor
						.getColumnIndex("Answer3")));
				m.setAnswer4(mCursor.getString(mCursor
						.getColumnIndex("Answer4")));
				m.setResult(mCursor.getString(mCursor.getColumnIndex("Result")));
				Ma.add(m);
				Log.i(TAG, "hoainhi " + Ma);

				mCursor.moveToNext();
			}

		}

	}

	public class CountTimer extends CountDownTimer {

		public CountTimer(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			mCountTime.setText("Time Out !");
		}

		@Override
		public void onTick(long millisUntilFinished) {
			long expectTime;
			if (checkpass == true) {
				mCountTime.setText("Time: " + millisUntilFinished / 1000);

			} else {
				expectTime = millisUntilFinished - 1000;
				mCountTime.setText("Time: " + expectTime / 1000);
			}
		}

	}
}
